package org.funtester.performance.books.chapter10.section3;

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.funtester.performance.books.chapter09.section3.UserBehavior;

/**
 * UserBehavior对象池化工厂类
 */
public class UserBehaviorFactory extends BasePooledObjectFactory<UserBehavior> {

    @Override
    public UserBehavior create() {
        return new UserBehavior();// 创建对象
    }

    @Override
    public PooledObject<UserBehavior> wrap(UserBehavior obj) {
        return new DefaultPooledObject<>(obj);// 包装对象,这里的obj是create()方法返回的对象
    }

    @Override
    public void passivateObject(PooledObject<UserBehavior> p) throws Exception {
        p.getObject().clear();// 清空对象
        super.passivateObject(p);
    }

}
